package com.xl.rentkeeper.act;

import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.TextView;

import com.xl.fuse.protos.account.User;
import com.xl.rentkeeper.App;
import com.xl.rentkeeper.R;
import com.xl.rentkeeper.business.UserLogic;
import com.xl.rentkeeper.util.DialogUtil;
import com.xl.rentkeeper.view.TitleBar;

/**
 * Created by Administrator on 2015/7/20.
 */
public class RegisterNo4Act extends BaseAct implements View.OnClickListener {
    private TextView mTVNext;
    private TextView mTVRoom;
    private TextView mTVGender;
    private TextView mTVName;
    private EditText mETNick;
    private User mUser;
    private Dialog mDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_register_no4);
        mUser = (User)getIntent().getSerializableExtra("user");
        //initToolBar();
        TitleBar mTitleBar = (TitleBar) findViewById(R.id.regiNo4_tl);
        initTitleBar(mTitleBar);
        initView();
    }
    private void initView(){
        mTVNext = (TextView) findViewById(R.id.tv_next);
        mTVRoom = (TextView) findViewById(R.id.et_room);
        mTVGender = (TextView) findViewById(R.id.et_gender);
        mTVName = (TextView) findViewById(R.id.et_name);
        mTVNext.setOnClickListener(this);
        mETNick = (EditText) findViewById(R.id.et_nick);
       /* if(mUser.room!=null){
            String room ="";
            if(!TextUtils.isEmpty(mUser.room.depName))
                room+=mUser.room.depName;
            if(!TextUtils.isEmpty(mUser.room.house))
                room+=mUser.room.house;
            if(!TextUtils.isEmpty(mUser.room.room))
                room+=mUser.room.room;
            mTVRoom.setText(room);
        }*/
        if(mUser.gender!=null){
            if(mUser.gender == 0){
                mTVGender.setText(R.string.male);
            }else{
                mTVGender.setText(R.string.female);
            }
        }

        if(mUser.name!=null){
            mTVName.setText(mUser.name);
        }
    }

    public String[] monitorEvents() {
        return new String[]{CMD_USER_SET_INFO};
    }

    private void settingUserInfo(){
        String nick = mETNick.getText().toString().trim();
        if(nick.length()==0){
            App.showToast(R.string.nickempty);
            return;
        }
        mDialog = DialogUtil.showProgressDialog(this, R.string.loading);
        User user =  new User.Builder().phone(mUser.phone).uin(mUser.uin).nickName(nick).build();
        UserLogic.getInstance().settingUserInfo(user);
    }

    @Override
    public void onClick(View v) {
        if (v == mTVNext){
            settingUserInfo();
        }
    }
    @Override
    public void onEvent(String event, boolean ret,String errInfo, Object[] data) {
        super.onEvent(event, ret, errInfo,data);
        if (CMD_USER_SET_INFO.equals(event)) {
            if (mDialog != null && mDialog.isShowing()) mDialog.dismiss();
//            if (ret) {
                Intent intent = new Intent(this,MainActivity.class);
                startActivity(intent);
                finish();
//            }
        }
    }
    @Override
    public boolean onBack(int keyCode, KeyEvent event) {
        DialogUtil.showBackDialog(this, R.string.tips, R.string.tips_back, R.string.sure, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                finish();
            }
        },R.string.cancel,null).show();
        return false;
    }
}
